package com.huawei.petapp2.category;


import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import androidx.annotation.Nullable;

import com.huawei.petapp2.R;
import com.huawei.petapp2.constant.HttpAddress;
import com.huawei.petapp2.entity.CategoryPrimary;
import com.huawei.petapp2.entity.Result;
import com.huawei.petapp2.fragment.BaseFragment;
import com.huawei.petapp2.network.api.ApiCategory;
import com.huawei.petapp2.network.core.ApiPath;
import com.huawei.petapp2.network.core.ResponseEntity;
import com.huawei.petapp2.network.entity.Filter;
import com.huawei.petapp2.search.SearchGoodsActivity;
import com.huawei.petapp2.utils.DateUtil;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnItemClick;

/**
 * 分类页面.
 */
public class CategoryFragment extends BaseFragment {

    public static CategoryFragment newInstance() {
        return new CategoryFragment();
    }

    @BindView(R.id.list_category)
    ListView categoryListView;
    @BindView(R.id.list_children)
    ListView childrenListView;

    private CategoryAdapter mCategoryAdapter;
    private ChildrenAdapter mChildrenAdapter;
    private List<com.huawei.petapp2.entity.CategoryPrimary> mData;
    private Context context;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View view = View.inflate(getActivity(),R.layout.fragment_category, null);

        context=getActivity();
      //  ButterKnife.bind(this,view);
        initView(view);
        //onClick(view);
        ButterKnife.bind(this,view);
        return view;
    }
    @Override
    public int getLayoutId() {
         return R.layout.fragment_category;
    }


    public int getContentViewLayout() {
        return R.layout.fragment_category;
    }

    @Override
    public void initView(View mView) {
        //设置标题
       /* new ToolbarWrapper(this)
                .setShowBack(false)
                .setShowTitle(false)
                .setCustomTitle(R.string.category_title);*/
        //左侧分类list
        categoryListView=mView.findViewById(R.id.list_category);
        childrenListView=mView.findViewById(R.id.list_children);

        mCategoryAdapter = new CategoryAdapter();
        categoryListView.setAdapter(mCategoryAdapter);
        //右侧分类下的二级分类
        mChildrenAdapter = new ChildrenAdapter();
        childrenListView.setAdapter(mChildrenAdapter);
        getData();
    }

    @SuppressLint("NonConstantResourceId")
    @OnItemClick(R.id.list_category)
    void onItemClick(int position) {
        System.out.println(position);
        chooseCategory(position);
    }

    @SuppressLint("NonConstantResourceId")
    @OnItemClick(R.id.list_children)
    void onChildrenItemClick(int position) {
        System.out.println(position);
        int categoryId = mChildrenAdapter.getItem(position).getCategoryId();
        navigateToSearch(categoryId);
    }

    /**
     * 获取分类数据
     */
    private void getData() {
        if (mData != null) {
            updateCategory(mData);
        } else {
            Result result = DateUtil.selectList(
                    HttpAddress.get(HttpAddress.Category(),"category-list"));
            System.out.println(result.getResult());
            List<CategoryPrimary>mData=DateUtil.getList5(result);
            System.out.println("所有的分类"+mData.size());
            //打印输出
            for(int i=0;i<mData.size();i++)
            {
                System.out.println("所有的分类"+mData.get(i).getCategoryName());
            }
            updateCategory(mData);
        }
    }

    /**
     * 更新分类数据
     */
    private void updateCategory(List<com.huawei.petapp2.entity.CategoryPrimary> mData) {
        for(int i=0;i<mData.size();i++)
        {
            System.out.println("所有的分类"+mData.get(i).getCategoryName());
        }
        mCategoryAdapter.reset(mData);
        chooseCategory(0);
    }

    /**
     * 选择分类
     *
     * @param position
     */
    private void chooseCategory(int position) {
        System.out.println(position);
        categoryListView.setItemChecked(position, true);
        mChildrenAdapter.reset(mCategoryAdapter.getItem(position).getCategories());
    }

    /**
     * 搜索
     * @param categoryId
     */
    private void navigateToSearch(int categoryId) {
        Filter filter = new Filter();
        filter.setCategoryId(categoryId);
        Intent intent = SearchGoodsActivity.getStartIntent(getContext(), categoryId);
        getActivity().startActivity(intent);
    }


    protected void onBusinessResponse(String apiPath, boolean success, ResponseEntity rsp) {
        if (!ApiPath.CATEGORY.equals(apiPath)) {
            throw new UnsupportedOperationException(apiPath);
        }
        if (success) {
            ApiCategory.Rsp categoryRsp = (ApiCategory.Rsp) rsp;
            mData = categoryRsp.getData();
            updateCategory(mData);
        }
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        inflater.inflate(R.menu.fragment_category, menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int itemId = item.getItemId();
        if (itemId == R.id.menu_search) {
            int position = categoryListView.getCheckedItemPosition();
            int categoryId = mCategoryAdapter.getItem(position).getCategoryId();
            navigateToSearch(categoryId);
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        // 请求失败的简化处理方案: 界面切换时触发重新请求.
        if (!hidden && mData == null) {
            Result result = DateUtil.selectList(
                    HttpAddress.get(HttpAddress.Category(),"category-list"));
            System.out.println(result.getResult());
            List<CategoryPrimary>mData=DateUtil.getList5(result);
        }
    }


}
